package com.example.basic01;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;

@Slf4j
public class CallByValueOrReference {

    @Test
    public void test() {
        int num1 = 10;
        int num2 = 20;

        swap(num1, num2);

        log.info("num1 = {}", num1);
        log.info("num2 = {}", num2);
    }

    public void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;

        log.info("a = {}", a);
        log.info("b = {}", b);
    }

    @Test
    public void main() {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(arr));
        change(arr);
        System.out.println(Arrays.toString(arr));
    }

    /*
        传个地址
     */
    public void change(int[] array) {
        // 将数组的第一个元素变为0
        array[0] = 0;
    }

    @Getter
    @Setter
    static class Student {
        private String name;

        public Student(String name) {
            this.name = name;
        }
    }

    @Test
    public void main1() {
        Student s1 = new Student("张三");
        Student s2 = new Student("李四");
        log.info("s1:{}", s1);
        log.info("s2:{}", s2);
        swap(s1, s2);
        log.info("s1:{}", s1.getName());
        log.info("s2:{}", s2.getName());
        log.info("s1:{}", s1);
        log.info("s2:{}", s2);
    }

    public void swap(Student x, Student y) {
        log.info("x:{}", x);
        log.info("y:{}", y);
        Student temp = x;
        x = y;
        y = temp;
        log.info("x:{}", x);
        log.info("y:{}", y);
        x.setName("test -_-");
        log.info("x:{}", x.getName());
        log.info("y:{}", y.getName());
    }
}
